/**
 * 日志实体
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Entity, 
  Column, 
  PrimaryGeneratedColumn, 
  CreateDateColumn 
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { LogType, LogLevel } from '../../../interfaces/log.interface';

@Entity('log')
export class Log {
  @ApiProperty({ description: '日志ID' })
  @PrimaryGeneratedColumn({ comment: '日志ID' })
  id: number;

  @ApiProperty({ description: '日志类型：1-操作日志 2-登录日志 3-错误日志' })
  @Column({ 
    type: 'smallint',
    comment: '日志类型：1-操作日志 2-登录日志 3-错误日志'
  })
  type: LogType;

  @ApiProperty({ description: '日志级别' })
  @Column({ 
    type: 'enum',
    enum: LogLevel,
    default: LogLevel.INFO,
    comment: '日志级别'
  })
  level: LogLevel;

  @ApiProperty({ description: '操作用户ID' })
  @Column({ 
    type: 'int',
    nullable: true,
    comment: '操作用户ID'
  })
  user_id: number;

  @ApiProperty({ description: '操作用户名' })
  @Column({ 
    length: 50,
    nullable: true,
    comment: '操作用户名'
  })
  username: string;

  @ApiProperty({ description: '操作IP' })
  @Column({ 
    length: 50,
    comment: '操作IP'
  })
  ip: string;

  @ApiProperty({ description: '请求方法' })
  @Column({ 
    length: 10,
    comment: '请求方法'
  })
  method: string;

  @ApiProperty({ description: '请求路径' })
  @Column({ 
    length: 200,
    comment: '请求路径'
  })
  path: string;

  @ApiProperty({ description: '请求参数' })
  @Column({ 
    type: 'text',
    nullable: true,
    comment: '请求参数'
  })
  params: string;

  @ApiProperty({ description: '操作结果' })
  @Column({ 
    type: 'text',
    nullable: true,
    comment: '操作结果'
  })
  result: string;

  @ApiProperty({ description: '错误信息' })
  @Column({ 
    type: 'text',
    nullable: true,
    comment: '错误信息'
  })
  error: string;

  @ApiProperty({ description: '执行时长(ms)' })
  @Column({ 
    type: 'int',
    comment: '执行时长(ms)'
  })
  duration: number;

  @ApiProperty({ description: '用户代理' })
  @Column({ 
    length: 500,
    nullable: true,
    comment: '用户代理'
  })
  user_agent: string;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn({ 
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间'
  })
  created_at: Date;
} 